package com.example.backstage.controller;

import com.example.backstage.service.IFileService;
import com.example.backstage.service.UserService;
import com.example.gok_mall.constUtil.PropertiesUtil;
import com.example.gok_mall.entity.User;
import com.example.gok_mall.reponseData.ResultData;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;


@RestController
@ResponseBody
@RequestMapping("/FileUp")
public class ProductFileUpController {

    @Autowired
    private UserService userService;

    @Autowired
    private IFileService iFileService;
    //springmvc文件上传接口
    /**
     * 单文件上传
     * 在springmvc中，实现文件上传需要借助MultiparFile对象，
     * 前端上传每个文件，都被封装到了此对象里
     */
    //springmvc文件上传接口
    @PostMapping("/upload.do")
    public ResultData upload(HttpSession session, @RequestParam(value = "upload_file", required = false) MultipartFile file, HttpServletRequest request) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        if (userService.checkAdminRole(user).isSuccess()) {
            String path = request.getSession().getServletContext().getRealPath("upload");
            String targetFileName = iFileService.upload(file, path);
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix") + targetFileName;

            Map fileMap = Maps.newHashMap();

            fileMap.put("uri", targetFileName);
            fileMap.put("url", url);
            return ResultData.createData(fileMap);

        } else {
            return ResultData.fail("当前登录者不是管理员，无权限操作");
        }
    }

    //富文本上传接口
    @PostMapping("/richtext_img_upload.do")
    public Map richtextImgUpload(HttpSession session, @RequestParam(value = "upload_file", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = Maps.newHashMap();
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {

            resultMap.put("success", false);
            resultMap.put("msg", "未登录，请先登录");
            return resultMap;
        }
        //富文本中对于返回值有自己的要求，我们使用是simditor 所以要按照simditor的要求进行返回

        if (userService.checkAdminRole(user).isSuccess()) {

            String path = request.getSession().getServletContext().getRealPath("upload");
            String targetFileName = iFileService.upload(file, path);

            if (StringUtils.isBlank(targetFileName)) {
                resultMap.put("success", false);
                resultMap.put("msg", "上传失败");
                return resultMap;
            }
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix") + targetFileName;

            resultMap.put("success", true);
            resultMap.put("msg", "上传成功");
            resultMap.put("ile_path", url);
            response.addHeader("Access-Control-Allow-Headers", "X-File-Name");
            return resultMap;

        } else {
            resultMap.put("success", false);
            resultMap.put("msg", "当前登录者不是管理员，无权限操作");
            return resultMap;
        }
    }
}
